package com.somiz.famille;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import org.hibernate.Session;

import com.gest.util.FileDirectoryUtil;
import com.gest.util.ImagePanel;
import com.somiz.main.Control;
import com.somiz.main.Principal;
import com.tech.hibernate.HibernateUtil;

public class FamilleForm extends JPanel {
	private JTextField T_nom;
	private JTextField T_lienImage;
	private static String nomImage;
	private static Long familleId;
	private ImagePanel imagePanel;

	/**
	 * Create the panel.
	 */
	public FamilleForm() {
		setLayout(null);

		JButton button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (errForm().equals("")) {
					if (familleId == null) {
						// creer
						Famille famille = new Famille();
						famille = FormVersFamille(famille);
						Session session = HibernateUtil.getSessionFactory()
								.openSession();
						if (Control.getFamilleDao().isExisteAvecNom(famille,
								session)) {
							JOptionPane
									.showMessageDialog(
											null,
											"cette famille existe deja dans votre base do donnees",
											"Erreur",
											JOptionPane.WARNING_MESSAGE);
						} else {
							Control.getFamilleDao().Creer(famille, session);
							familleId = famille.getId();
						}
						session.close();
					} else {
						// modif
						Session session = HibernateUtil.getSessionFactory()
								.openSession();
						Famille famille = new Famille();
						famille = FormVersFamille(famille);

						if (Control.getFamilleDao().isExisteAvecNom(famille,
								session)) {
							if (familleId.equals(Control.getFamilleDao()
									.TrouverAvecNom(famille, session).getId())) {
								famille = Control.getFamilleDao()
										.TrouverAvecId(familleId, famille,
												session);
								famille = FormVersFamille(famille);
								Control.getFamilleDao().modifier(famille,
										session);
							} else {
								JOptionPane
										.showMessageDialog(
												null,
												"cette famille existe deja dans votre base de donnees",
												"Erreur",
												JOptionPane.WARNING_MESSAGE);
							}

						} else {
							famille = Control.getFamilleDao().TrouverAvecId(
									familleId, famille, session);
							famille = FormVersFamille(famille);
							Control.getFamilleDao().modifier(famille, session);
						}
						session.close();
					}
				} else {
					JOptionPane.showMessageDialog(null, errForm(), "Erreur",
							JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		button.setIcon(new ImageIcon(FamilleForm.class
				.getResource("/cc/black/png/save_icon&32.png")));
		button.setPreferredSize(new Dimension(50, 50));
		button.setMnemonic(KeyEvent.VK_ENTER);
		button.setBackground(Color.WHITE);
		button.setBounds(10, 11, 50, 50);
		add(button);

		JButton button_1 = new JButton("");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				Control.getFamilleForm().vider();
				Control.getPrincipal()
						.getTabbedPane()
						.addTab("Famille",
								new ImageIcon(
										Principal.class
												.getResource("/cc/black/png/cert_icon&16.png")),
								Control.getFamilleForm(), null);
				Control.getPrincipal()
						.getTabbedPane()
						.setSelectedIndex(
								Control.getPrincipal().getTabbedPane()
										.getTabCount() - 1);
				session.close();
			}
		});
		button_1.setIcon(new ImageIcon(FamilleForm.class
				.getResource("/images/png/Add32.png")));
		button_1.setPreferredSize(new Dimension(50, 50));
		button_1.setMnemonic(KeyEvent.VK_ENTER);
		button_1.setBackground(Color.WHITE);
		button_1.setBounds(70, 11, 50, 50);
		add(button_1);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 81, 393, 2);
		add(separator);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Marque :",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 101, 393, 87);
		add(panel);

		JLabel lblNomDeLa = new JLabel("Nom de la famille:");
		lblNomDeLa.setBounds(29, 26, 124, 28);
		panel.add(lblNomDeLa);

		T_nom = new JTextField();
		T_nom.setColumns(10);
		T_nom.setBounds(163, 23, 220, 34);
		panel.add(T_nom);

		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setBounds(95, 199, 221, 225);
		add(panel_1);

		imagePanel = new ImagePanel();
		imagePanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		imagePanel.setBackground(Color.WHITE);
		imagePanel.setBounds(36, 11, 149, 154);
		panel_1.add(imagePanel);

		T_lienImage = new JTextField();
		T_lienImage.setColumns(10);
		T_lienImage.setBounds(10, 176, 165, 28);
		panel_1.add(T_lienImage);

		JButton button_2 = new JButton("");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				int retour = chooser.showOpenDialog(Control.getPrincipal());
				if (retour == JFileChooser.APPROVE_OPTION) {
					T_lienImage.setText(chooser.getSelectedFile()
							.getAbsolutePath());
					nomImage = chooser.getSelectedFile().getName();
					imagePanel.loadImage(chooser.getSelectedFile()
							.getAbsolutePath());
				}
			}
		});
		button_2.setIcon(new ImageIcon(FamilleForm.class
				.getResource("/cc/black/png/zoom_icon&16.png")));
		button_2.setPreferredSize(new Dimension(50, 50));
		button_2.setMnemonic(KeyEvent.VK_ENTER);
		button_2.setBackground(Color.WHITE);
		button_2.setBounds(185, 176, 26, 28);
		panel_1.add(button_2);

	}

	public void FamilleVersForm(Famille famille) {
		T_lienImage.setText(famille.getLienImage());
		imagePanel.loadImage(famille.getLienImage());
		T_nom.setText(famille.getNom());
	}

	public Famille FormVersFamille(Famille famille) {
		famille.setNom(T_nom.getText());
		if (!T_lienImage.getText().toString()
				.equals("images/imageDefaut/default.png")) {
			famille.setLienImage(T_lienImage.getText());
			famille.setLienImage("images/imageFamilles/" + nomImage);
			FileDirectoryUtil.copyFile(T_lienImage.getText(),
					"images/imageFamilles/" + nomImage);
		} else {
			famille.setLienImage(T_lienImage.getText());
		}
		return famille;
	}

	public String errForm() {
		String err = "";
		if (T_nom.getText().equals("")) {
			err = err + "il faut donner un nom pour la famille\n";
		}
		return err;
	}

	public void vider() {
		T_lienImage.setText("images/imageDefaut/default.png");
		T_nom.setText("");
		imagePanel.loadImage("images/imageDefaut/default.png");
		familleId = null;
	}
}
